package com.smart.campus.school.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.smart.campus.system.domain.SysUser;
import com.smart.campus.system.service.ISysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.smart.campus.school.mapper.AdjustCourseAdminMapper;
import com.smart.campus.school.domain.AdjustCourseAdmin;
import com.smart.campus.school.service.IAdjustCourseAdminService;
import com.smart.campus.common.core.text.Convert;

/**
 * 调课管理员设置Service业务层处理
 * 
 * @author Zhihe.Li
 * @date 2020-11-03
 */
@Service
public class AdjustCourseAdminServiceImpl implements IAdjustCourseAdminService 
{
    @Autowired
    private AdjustCourseAdminMapper adjustCourseAdminMapper;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询调课管理员设置
     * 
     * @param id 调课管理员设置ID
     * @return 调课管理员设置
     */
    @Override
    public AdjustCourseAdmin selectAdjustCourseAdminById(Long id)
    {
        return adjustCourseAdminMapper.selectAdjustCourseAdminById(id);
    }

    /**
     * 查询调课管理员设置列表
     * 
     * @param adjustCourseAdmin 调课管理员设置
     * @return 调课管理员设置
     */
    @Override
    public List<AdjustCourseAdmin> selectAdjustCourseAdminList(AdjustCourseAdmin adjustCourseAdmin)
    {

        List<AdjustCourseAdmin> adminList = adjustCourseAdminMapper.selectAdjustCourseAdminList(adjustCourseAdmin);
        List<SysUser> userList = sysUserService.selectUserList(new SysUser());
        Map<Long,String> userMap =  userList.stream().collect(Collectors.toMap(SysUser::getUserId, SysUser::getUserName));
        for (AdjustCourseAdmin courseAdmin : adminList) {
            String adminIds = courseAdmin.getAdminIds();
            if(StringUtils.isNotEmpty(adminIds)){

                String[] adminIdArray = adminIds.split(",");
                for (int i = 0; i < adminIdArray.length; i++) {
                    adminIdArray[i] = userMap.get(Long.valueOf(adminIdArray[i]));
                }
                courseAdmin.setAdminNames(Arrays.toString(adminIdArray));
            }
        }
        return adminList;
    }

    /**
     * 新增调课管理员设置
     * 
     * @param adjustCourseAdmin 调课管理员设置
     * @return 结果
     */
    @Override
    public int insertAdjustCourseAdmin(AdjustCourseAdmin adjustCourseAdmin)
    {
        return adjustCourseAdminMapper.insertAdjustCourseAdmin(adjustCourseAdmin);
    }

    /**
     * 修改调课管理员设置
     * 
     * @param adjustCourseAdmin 调课管理员设置
     * @return 结果
     */
    @Override
    public int updateAdjustCourseAdmin(AdjustCourseAdmin adjustCourseAdmin)
    {
        return adjustCourseAdminMapper.updateAdjustCourseAdmin(adjustCourseAdmin);
    }

    /**
     * 删除调课管理员设置对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteAdjustCourseAdminByIds(String ids)
    {
        return adjustCourseAdminMapper.deleteAdjustCourseAdminByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除调课管理员设置信息
     * 
     * @param id 调课管理员设置ID
     * @return 结果
     */
    @Override
    public int deleteAdjustCourseAdminById(Long id)
    {
        return adjustCourseAdminMapper.deleteAdjustCourseAdminById(id);
    }
}
